//
//  yxpGCDVController.m
//  BlockTest
//
//  Created by 杨小平 on 14-8-16.
//  Copyright (c) 2014年 yangxiaoping. All rights reserved.
//

#import "yxpGCDVC.h"

@interface SubObject : NSObject

@end


@interface yxpGCDVC ()
{
    UIImageView *_imageView;
}


@end

@implementation yxpGCDVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //初始化一_ImageView
    _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 70, 300, self.view.frame.size.height-84)];
    _imageView.backgroundColor=[UIColor grayColor];
    _imageView.animationDuration=3.0;
    _imageView.animationRepeatCount=0;
    [self.view addSubview:_imageView];
    
    //后台执行队列
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *url1=@"http://h.hiphotos.baidu.com/image/w%3D230/sign=b2d5c289123853438ccf8022a311b01f/91ef76c6a7efce1b1ae9f92fad51f3deb58f6510.jpg";
        NSString *url2=@"http://h.hiphotos.baidu.com/image/pic/item/d058ccbf6c81800aae834e8bb33533fa838b47d5.jpg";
        NSString *url3=@"http://d.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494eec3ba65132ff5e0fe99257e1b.jpg";
        NSString *url4=@"http://g.hiphotos.baidu.com/image/pic/item/a6efce1b9d16fdfa81f4ace4b68f8c5494ee7b1b.jpg";
        NSString *url5=@"http://g.hiphotos.baidu.com/image/pic/item/d6ca7bcb0a46f21f70031fdbf4246b600c33ae07.jpg";
        NSArray *array=[[NSArray alloc] initWithObjects:url1,url2,url3,url4,url5, nil];
        
        NSMutableArray *imageArray=[[NSMutableArray alloc] initWithCapacity:20];
        for (NSString *string in array) {
            //下载图片
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:string]];
            UIImage *image = [UIImage imageWithData:data];
            [imageArray addObject:image];
        }
        _imageView.animationImages=imageArray;
        
            //主线程执行方法
            dispatch_async(dispatch_get_main_queue(), ^{
                [_imageView  startAnimating];
            });
        });
//
//    
//    // 自定义dispatch_queue_t
//    dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
//    dispatch_async(urls_queue, ^{
//        　 　//your code
//    });
//    //如果在非ARC项目中，要记得释放
//    //dispatch_release(urls_queue);
    
//    //线程汇总通知，等所有线程执行完成后，会通知notify线程
//    dispatch_group_t group = dispatch_group_create();
//    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
//        // 并行执行的线程一
//        for (int i=0; i<10; i++) {
//            NSLog(@"---------%i",i);
//        }
//    });
//    dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
//        // 并行执行的线程二
//        for (int i=10; i<20; i++) {
//            NSLog(@"+++++++++%i",i);
//        }
//    });
//    dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
//        // 汇总结果
//        NSLog(@"完成");
//    });
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_imageView stopAnimating];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
